package com.mall.admin.service;

import cn.hutool.core.bean.BeanUtil;


import com.mall.commons.utils.PageResult;
import com.mall.commons.utils.R;
import com.mall.domain.PmsProductCategory;
import com.mall.domain.PmsProductCategoryAttributeRelation;
import com.mall.interface1.api.PmsProductCategoryApi;
import com.mall.interface1.api.PmsProductCategoryAttributeRelationApi;
import com.mall.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductCategoryService {

    @DubboReference
    private PmsProductCategoryApi pmsProductCategoryApi;
    @DubboReference
    private PmsProductCategoryAttributeRelationApi pmsProductCategoryAttributeRelationApi;

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    public R listWithChildren() {
        List<PmsProductCategoryVo> pmsProductCategoryVoList = pmsProductCategoryApi.findAll();
        return R.success("",pmsProductCategoryVoList);
    }

    /**
     * 添加商品分类
     * @param categoryVo
     * @return
     */
    public R create(PmsProductCategoryVo categoryVo) {
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        BeanUtil.copyProperties(categoryVo,pmsProductCategory);

        Long pmsProductCategoryId = pmsProductCategoryApi.add(pmsProductCategory);

        List<Long> productAttributeIdList = categoryVo.getProductAttributeIdList();

        for (Long productAttributeId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
            pmsProductCategoryAttributeRelation.setProductCategoryId(pmsProductCategoryId);
            pmsProductCategoryAttributeRelation.setProductAttributeId(productAttributeId);

            pmsProductCategoryAttributeRelationApi.add(pmsProductCategoryAttributeRelation);
        }

        return R.success("",null);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    public R deleteById(Long id) {
        pmsProductCategoryApi.deleteById(id);
        pmsProductCategoryAttributeRelationApi.deleteByCategoryId(id);
        return R.success("",null);
    }

    /**
     * 分页查询商品分类
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @return
     */
    public R listParentId(Integer pageNum, Integer pageSize, Long parentId) {
        PageResult pageResult = pmsProductCategoryApi.listParentId(pageNum,pageSize,parentId);
        return R.success("",pageResult);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     * @return
     */
    public R updateNavStatus(List<Long> ids, Integer navStatus) {
        for (Long id : ids) {
            PmsProductCategory pmsProductCategory = pmsProductCategoryApi.findById(id);
            pmsProductCategory.setNavStatus(navStatus);
            pmsProductCategoryApi.updateById(pmsProductCategory);
        }
        return R.success("",null);
    }

    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    public R updateShowStatus(List<Long> ids, Integer showStatus) {
        for (Long id : ids) {
            PmsProductCategory pmsProductCategory = pmsProductCategoryApi.findById(id);
            pmsProductCategory.setShowStatus(showStatus);
            pmsProductCategoryApi.updateById(pmsProductCategory);
        }
        return R.success("",null);
    }

    /**
     * 修改商品分类
     * @param categoryVo
     * @param id
     * @return
     */
    public R updateById(PmsProductCategoryVo categoryVo, Long id) {
        categoryVo.setId(id);
        pmsProductCategoryApi.updateById(categoryVo);
        return R.success("",null);
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    public R findById(Long id) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryApi.findById(id);
        return R.success("",pmsProductCategory);
    }
}
